package com.hjm.project.terminal.pos.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.google.common.collect.Maps;
import com.hjm.common.utils.fastdfs.FastDFSClient;
import com.hjm.common.utils.security.ShiroUtils;
import com.hjm.common.utils.yh.Utils;
import com.hjm.project.clientImg.clientImg.domain.ClientImg;
import com.hjm.project.clientImg.clientImg.mapper.ClientImgMapper;
import com.hjm.project.terminal.pos.domain.AgentApplyPos;
import com.hjm.project.terminal.pos.mapper.AgentApplyPosMapper;
import com.hjm.project.terminal.pos.service.IAgentApplyPosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hjm.common.utils.text.Convert;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 服务商划拨、撤销机具申请信息Service业务层处理
 * 
 * @author LOVE
 * @date 2020-11-27
 */
@Service
public class AgentApplyPosServiceImpl implements IAgentApplyPosService
{
    @Autowired
    private AgentApplyPosMapper agentApplyPosMapper;
    @Autowired
    private ClientImgMapper clientImgMapper;

    /**
     * 查询服务商划拨、撤销机具申请信息
     * 
     * @param id 服务商划拨、撤销机具申请信息ID
     * @return 服务商划拨、撤销机具申请信息
     */
    @Override
    public AgentApplyPos selectAgentApplyPosById(Long id)
    {
        return agentApplyPosMapper.selectAgentApplyPosById(id);
    }

    /**
     * 查询服务商划拨、撤销机具申请信息列表
     * 
     * @param agentApplyPos 服务商划拨、撤销机具申请信息
     * @return 服务商划拨、撤销机具申请信息
     */
    @Override
    public List<AgentApplyPos> selectAgentApplyPosList(AgentApplyPos agentApplyPos)
    {
        return agentApplyPosMapper.selectAgentApplyPosList(agentApplyPos);
    }

    /**
     * 新增服务商划拨、撤销机具申请信息
     * 
     * @param agentApplyPos 服务商划拨、撤销机具申请信息
     * @return 结果
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Map<String,String> insertAgentApplyPos(MultipartFile payPic, AgentApplyPos agentApplyPos)
    {
        Map<String,String> result=new HashMap<>(16);
        result.put("code","00");
        result.put("msg","操作成功");
        int posNumber=agentApplyPosMapper.getAllPosNumber(agentApplyPos.getPosType());
        if (Integer.parseInt(agentApplyPos.getApplyNum())>posNumber){
            result.put("code","01");
            result.put("msg","所剩可划拨机具不足，目前机具数量为"+posNumber);
            return result;
        }
        if ("0".equals(agentApplyPos.getPosType())){
            Map sim=agentApplyPosMapper.getSimReturn(agentApplyPos.getXagentNum());
            if (sim == null || sim.size() == 0){
                result.put("code","01");
                result.put("msg","划拨所选服务商未配置流量卡返现");
                return result;
            }
        }
        if (payPic==null){
            result.put("code","01");
            result.put("msg","请上传充值凭证！");
            return result;
        }
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String [] date=sdf.format(new Date()).split(" ");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DAY_OF_MONTH, 3);
        agentApplyPos.setApplyType("0");
        agentApplyPos.setFinanceStatus("2");
        agentApplyPos.setAgentNum("0");
        agentApplyPos.setOverdueDate(new SimpleDateFormat("yyyy-MM-dd").format(c.getTime()));
        agentApplyPos.setHandleRs("0");
        agentApplyPos.setResult("2");
        agentApplyPos.setOrderNum(Utils.formateDate(4));
        agentApplyPos.setApplyDate(date[0]);
        agentApplyPos.setApplyTime(date[1]);
        agentApplyPos.setAgentName("总公司");
        //判断格式是不是图片格式jpg png gif
        ClientImg clientImg=new ClientImg();
        String fileName =  payPic.getOriginalFilename();
        if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")) {
            //上传图片
            Map<String, String> metaList = Maps.newHashMap();
            metaList.put("fileName",fileName);
            metaList.put("fileType", fileName.substring(fileName.lastIndexOf(".")+1));
            String fid = FastDFSClient.uploadFile(payPic, payPic.getOriginalFilename(), metaList);
            String fastGroup = fid.substring(0, 17);
            clientImg.setClientno(agentApplyPos.getOrderNum());
            clientImg.setFileid(fid);
            clientImg.setFastdfsGroup(fastGroup);
            clientImg.setCreatename(ShiroUtils.getLoginName());
            clientImg.setType(Long.valueOf("46"));
            clientImg.setObjType(Long.valueOf("1"));
        }
        int i=clientImgMapper.insertclientImg(clientImg);
        if (i>0){
            agentApplyPosMapper.insertAgentApplyPos(agentApplyPos);
        }
        return result;
    }

    /**
     * 修改服务商划拨、撤销机具申请信息
     * 
     * @param agentApplyPos 服务商划拨、撤销机具申请信息
     * @return 结果
     */
    @Override
    public int updateAgentApplyPos(AgentApplyPos agentApplyPos)
    {
        return agentApplyPosMapper.updateAgentApplyPos(agentApplyPos);
    }

    /**
     * 删除服务商划拨、撤销机具申请信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteAgentApplyPosByIds(String ids)
    {
        return agentApplyPosMapper.deleteAgentApplyPosByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除服务商划拨、撤销机具申请信息信息
     * 
     * @param id 服务商划拨、撤销机具申请信息ID
     * @return 结果
     */
    @Override
    public int deleteAgentApplyPosById(Long id)
    {
        return agentApplyPosMapper.deleteAgentApplyPosById(id);
    }

    /**
     * 查询服务商划拨、撤销机具申请信息列表
     *
     * @param agentApplyPos 服务商划拨、撤销机具申请信息
     * @return 服务商划拨、撤销机具申请信息
     */
    @Override
    public List<AgentApplyPos> selectAgentApplyPosFList(AgentApplyPos agentApplyPos)
    {
        return agentApplyPosMapper.selectAgentApplyPosFList(agentApplyPos);
    }

}
